//
//  ByViewShopsDeatilActivity.m
//  chequanbao_ios
//
//  Created by pro on 17/5/9.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewShopsDeatilActivity.h"

@implementation ByViewShopsDeatilActivity

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.layer.cornerRadius = 10;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setActivitys:(NSArray<NSDictionary *> *)activitys {
    
    _activitys = activitys;
    
    [self setNeedsLayout];
}
- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    /*
     模拟数据
     */
//    NSArray *activitys = @[@{@"title":@"优惠1",
//                             @"detail":@"详情详情详情详情详情详情详情详情详情"},
//                           @{@"title":@"优惠2",
//                             @"detail":@"详情详情详情详情"},
//                           @{@"title":@"优惠3",
//                             @"detail":@"详情详情详详情详情详情详详情详情详情详详情详情详情详详情详情详情详情详情"},
//                           @{@"title":@"优惠4",
//                             @"detail":@"详情详情详情详情详情详详情详情详情详详情详情详情详详情详情详情详详情详情详情详详情详情详情详详情详情详情详详情详情详情详详情详情详情详情详情详情详情"}];
    NSArray *activitys = self.activitys;
    
   
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    
    if (activitys.count == 0) {
        self.height = 0;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 0.01), dispatch_get_main_queue(), ^{
            [[NSNotificationCenter defaultCenter] postNotificationName:KDetailActivityDidLayout object:nil];
        });
        return;
    }
    
    UILabel *infoTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.width, 20)];
    infoTitleLabel.text = @"商家活动";
    infoTitleLabel.font = FONT(15);
    [self addSubview:infoTitleLabel];
    
    CGFloat x, y, width, height;
    CGFloat margin = 10, offset = infoTitleLabel.bottom;
    for (int i = 0; i < activitys.count; i ++) {
        
        NSDictionary *dic = activitys[i];
        
        width = self.width;
        height = 0;
        x = 0;
        y = offset;
        UIControl *basalView = [[UIControl alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [self addSubview:basalView];
        
        x = margin;
        y = margin;
//        width = 60;
//        height = 45;
        width = 90;
        height = 60;
        UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [iconView setImageWithURL:[NSURL URLWithString:BOYE_PICTURE_VIEW_URL(dic[@"img"])] placeholderImage:DEFAULT_IMAGE completed:nil];
        [basalView addSubview:iconView];
        
        x = iconView.right + margin;
        y = iconView.top;
        width = basalView.width - margin - x;
        height = [dic[@"title"] getRectOfStringWithSize:CGSizeMake(width, 10000) font:16].size.height;
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        titleLabel.numberOfLines = 0;
        titleLabel.adjustsFontSizeToFitWidth = YES;
        titleLabel.font = FONT(16);
        titleLabel.text = dic[@"title"];
        titleLabel.textColor = [UIColor blackColor];
        titleLabel.textAlignment = NSTextAlignmentLeft;
        [basalView addSubview:titleLabel];
        
        x = titleLabel.left;
        y = titleLabel.bottom + margin / 2;
        width = titleLabel.width;
        height = [dic[@"detail"] getRectOfStringWithSize:CGSizeMake(width, 10000) font:14].size.height;
        UILabel *detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        detailLabel.numberOfLines = 0;
        detailLabel.adjustsFontSizeToFitWidth = YES;
        detailLabel.font = FONT(14);
        detailLabel.text = dic[@"detail"];
        detailLabel.textColor = [UIColor lightGrayColor];
        detailLabel.textAlignment = NSTextAlignmentLeft;
        [basalView addSubview:detailLabel];
        
        
        if (i < activitys.count - 1) {
            x = margin;
//            y = detailLabel.bottom + margin;
            y = MAX(detailLabel.bottom, iconView.bottom) + margin;
            width = basalView.width - x * 2;
            height = 1;
            UIView *sepLine = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
            sepLine.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
            [basalView addSubview:sepLine];
        }else{
            x = margin;
            y = MAX(detailLabel.bottom, iconView.bottom) + margin;
            width = basalView.width - x * 2;
            height = 1;
            UIView *sepLine = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, 1)];
            sepLine.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
            sepLine.hidden = YES;
            [basalView addSubview:sepLine];
        }
        
        basalView.height = basalView.subviews.lastObject.bottom;
        offset += basalView.height;
    }
    
    self.height = self.subviews.lastObject.bottom;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 0.01), dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:KDetailActivityDidLayout object:nil];
    });


}
@end
